
package com.jf.cloud.multishop.controller.admin;

import cn.hutool.core.date.DateUtil;
import com.jf.cloud.common.constant.Constant;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.response.ResponseEnum;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.common.util.ExcelUtil;
import com.jf.cloud.multishop.bo.mongo.MongoShopWalletLogBO;
import com.jf.cloud.multishop.dto.ShopWalletLogSearchDTO;
import com.jf.cloud.multishop.service.ShopWalletLogService;
import com.jf.cloud.multishop.service.ShopWalletService;
import com.jf.cloud.multishop.vo.ShopWalletLogExcelVO;
import com.jf.cloud.multishop.vo.ShopWalletVO;
import com.jf.cloud.multishop.vo.mongo.MongoShopWalletLogVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 店铺钱包日志
 *
 * @author zz
 * @date 2021/6/8 19:46
 */
@RestController("adminShopWalletController")
@RequestMapping("/mp/shop_wallet")
@Tag(name = "admin-店铺钱包")
public class ShopWalletController {

    @Autowired
    private ShopWalletLogService shopWalletLogService;

    @Autowired
    private ShopWalletService shopWalletService;

    @GetMapping("/page")
    @Operation(summary = "分页查询钱包记录信息", description = "分页查询")
    public ServerResponseEntity<PageVO<MongoShopWalletLogBO>> getShopWalletLogPage(PageDTO pageDTO, @ParameterObject ShopWalletLogSearchDTO shopWalletLogSearchDTO) {
        Long shopId = AuthUserContext.get().getTenantId();
        if (!Objects.equals(shopId, Constant.PLATFORM_SHOP_ID)) {
            shopWalletLogSearchDTO.setShopId(shopId);
        }
        PageVO<MongoShopWalletLogBO> page = shopWalletLogService.pageByParam(pageDTO, shopWalletLogSearchDTO);
        return ServerResponseEntity.success(page);
    }

    @GetMapping("/get_all_shop_wallet")
    @Operation(summary = "查看所有店铺钱包总信息")
    public ServerResponseEntity<ShopWalletVO> getAllShopWalletVoByShopId(@ParameterObject ShopWalletLogSearchDTO shopWalletLogSearchDTO) {
        ShopWalletVO shopWallet = shopWalletService.getAllShop(shopWalletLogSearchDTO);
        return ServerResponseEntity.success(shopWallet);
    }

    @GetMapping("/page_shop_wallet_by_time")
    @Operation(summary = "分页查看店铺钱包总信息")
    public ServerResponseEntity<PageVO<ShopWalletVO>> pageShopWalletByTime(PageDTO pageDTO, @ParameterObject ShopWalletLogSearchDTO shopWalletLogSearchDTO) {
        PageVO<ShopWalletVO> pageShopWalletByTime = shopWalletService.pageShopWalletByTime(pageDTO, shopWalletLogSearchDTO);
        return ServerResponseEntity.success(pageShopWalletByTime);
    }

    @GetMapping("/page_all_shop")
    @Operation(summary = "查看所有店铺的日志")
    public ServerResponseEntity<PageVO<MongoShopWalletLogBO>> getAllShopWalletLogPage(PageDTO pageDTO, @ParameterObject ShopWalletLogSearchDTO shopWalletLogSearchDTO) {
        PageVO<MongoShopWalletLogBO> page = shopWalletLogService.pageAllShop(pageDTO, shopWalletLogSearchDTO);
        return ServerResponseEntity.success(page);
    }

    @GetMapping("/get_shop_wallet_log_form")
    @Operation(summary = "平台导出店铺结算报表", description = "平台导出店铺结算报表")
    public ServerResponseEntity<Void> getShopWalletLogForm(HttpServletResponse response, @ParameterObject ShopWalletLogSearchDTO shopWalletLogSearchDTO) {
        List<ShopWalletLogExcelVO> list = shopWalletLogService.listShopWalletLogExcel(shopWalletLogSearchDTO);
        ExcelUtil.soleExcel(response, list, ShopWalletLogExcelVO.EXCEL_NAME, ShopWalletLogExcelVO.MERGE_ROW_INDEX, ShopWalletLogExcelVO.MERGE_COLUMN_INDEX, ShopWalletLogExcelVO.class);
        return ServerResponseEntity.success();
    }

    @GetMapping("/soldExcel")
    @Operation(summary = "商家导出店铺结算报表", description = "商家导出店铺结算报表")
    public ServerResponseEntity<Void> soldExcel(HttpServletResponse response, @ParameterObject ShopWalletLogSearchDTO shopWalletLogSearchDTO) {
        Long shopId = AuthUserContext.get().getTenantId();
        if (!Objects.equals(shopId, Constant.PLATFORM_SHOP_ID)) {
            shopWalletLogSearchDTO.setShopId(shopId);
        }
        List<ShopWalletLogExcelVO> list = shopWalletLogService.listShopWalletLogExcel(shopWalletLogSearchDTO);
        ExcelUtil.soleExcel(response, list, ShopWalletLogExcelVO.EXCEL_NAME, ShopWalletLogExcelVO.MERGE_ROW_INDEX, ShopWalletLogExcelVO.MERGE_COLUMN_INDEX, ShopWalletLogExcelVO.class);
        return ServerResponseEntity.success();
    }

    @GetMapping("/get_shop_wallet")
    @Operation(summary = "查看店铺钱包信息", description = "根据店铺id查看店铺钱包信息")
    public ServerResponseEntity<ShopWalletVO> getShopWalletVoByShopId() {
        Long shopId = AuthUserContext.get().getTenantId();
        if (Objects.equals(shopId, Constant.PLATFORM_SHOP_ID)) {
            return ServerResponseEntity.success(shopWalletLogService.getPlatformWallet());
        }
        ShopWalletVO shopWallet = shopWalletService.getByShopId(shopId);
        return ServerResponseEntity.success(shopWallet);
    }

    @GetMapping("/sumToday")
    @Operation(summary = "查询今日商家钱包统计", description = "查询今日商家钱包统计")
    public ServerResponseEntity<MongoShopWalletLogVO> sumToday() {
        Long shopId = AuthUserContext.get().getTenantId();
        if (Objects.equals(shopId, Constant.PLATFORM_SHOP_ID)) {
            return ServerResponseEntity.fail(ResponseEnum.UNAUTHORIZED);
        }
        MongoShopWalletLogVO page = shopWalletLogService.listActualByHour(AuthUserContext.get().getTenantId(),
                DateUtil.beginOfDay(DateUtil.date()), DateUtil.endOfDay(DateUtil.date()));
        return ServerResponseEntity.success(page);
    }


    @GetMapping("/shop_wallet_by_area")
    @Operation(summary = "统计区县店铺数量和收入(数据大屏)")
    public ServerResponseEntity<List<Map<String, Object>>> page_shop_wallet_by_time_by_area(@RequestParam("areaId") Long areaId, @RequestParam("level") Integer level) {
        return ServerResponseEntity.success(shopWalletService.shop_wallet_by_area(areaId, level));
    }
}
